package org.friping.mytool.helper;

import android.content.Context;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.androidannotations.annotations.EBean;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

@EBean
public class JsonHelper {

    private ObjectMapper objectMapper = new ObjectMapper();

    public Map readMapFromAssets(Context context, String path) {
        try {
            InputStream is = context.getAssets().open(path);
            Map map = objectMapper.readValue(is, Map.class);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public List readListFromAssets(Context context, String path) {
        try {
            InputStream is = context.getAssets().open(path);
            List list = objectMapper.readValue(is, List.class);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public List readListFromString(String value) {
        try {
            List list = objectMapper.readValue(value, List.class);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public Map readMapFromString(String value) {
        try {
            Map map = objectMapper.readValue(value, Map.class);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
